﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Utilities.Menu.UIElements
{
    public class Box
    {
        Texture2D upper, lower, middle;
        Rectangle u_rect, l_rect, m_rect;
        Vector2 dest_position, dest_dimensions, init_position, init_dimensions;
        float interpolator, alpha;
        Color middle_color;
        bool inTransition = false, personalized_middle_panel = false, isRightLeftBorder = false;

        public Box(Texture2D[] up_cent_low_textures, Vector2 middle_position, Vector2 middle_dimensions)
        {
            interpolator = 0.0f;

            upper = up_cent_low_textures[0];
            middle = up_cent_low_textures[1];
            lower = up_cent_low_textures[2];

            u_rect = new Rectangle((int)middle_position.X, (int)middle_position.Y - upper.Height, (int)middle_dimensions.X, upper.Height);
            m_rect = new Rectangle((int)middle_position.X, (int)middle_position.Y, (int)middle_dimensions.X, (int)middle_dimensions.Y);
            l_rect = new Rectangle((int)middle_position.X, (int)middle_position.Y + (int)middle_dimensions.Y, (int)middle_dimensions.X, lower.Height);
        }

        public Box(Texture2D[] up_cent_low_textures, Vector2 middle_position, Vector2 middle_dimensions, Color middle_panel_color, float middle_panel_alpha)
            : this(up_cent_low_textures, middle_position, middle_dimensions)
        {
            middle_color = middle_panel_color;
            alpha = middle_panel_alpha;

            personalized_middle_panel = true;
        }

        public void ChangeMiddlePosition(Vector2 new_position)
        {
            int new_x = (int)(new_position.X), new_y = (int)(new_position.Y);
            u_rect = new Rectangle(new_x, new_y - upper.Height, u_rect.Width, upper.Height);
            m_rect = new Rectangle(new_x, new_y, m_rect.Width, m_rect.Height);
            l_rect = new Rectangle(new_x, new_y + m_rect.Height, l_rect.Width, lower.Height);
        }

        public void ChangeMiddleDimensions(Vector2 new_dimensions)
        {
            int new_w = (int)(new_dimensions.X), new_h = (int)(new_dimensions.Y);
            u_rect = new Rectangle(u_rect.X, u_rect.Y, new_w, upper.Height);
            m_rect = new Rectangle(m_rect.X, m_rect.Y, new_w, new_h);
            l_rect = new Rectangle(m_rect.X, m_rect.Y + m_rect.Height, new_w, lower.Height);
        }

        public void ChangeMiddleDimensioneAndPosition(Vector2 new_position, Vector2 new_dimension)
        {
            ChangeMiddlePosition(new_position);
            ChangeMiddleDimensions(new_dimension);
        }

        public void StartTransition(Vector2 final_position, Vector2 final_dimensions)
        {
            dest_position = final_position;
            dest_dimensions = final_dimensions;
            init_position = new Vector2(m_rect.X, m_rect.Y);
            init_dimensions = new Vector2(m_rect.Width, m_rect.Height);
            inTransition = true;
        }

        public void Update(float dt)
        {
            if (inTransition)
            {
                interpolator += dt;
                if (interpolator <= 1.0f)
                    ChangeMiddleDimensioneAndPosition(Vector2.SmoothStep(init_position, dest_position, interpolator),
                        Vector2.SmoothStep(init_dimensions, dest_dimensions, interpolator));
                else
                {
                    inTransition = false;
                    interpolator = 0.0f;
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (!personalized_middle_panel)
            {
                spriteBatch.Draw(upper, u_rect, Color.White);
                spriteBatch.Draw(middle, m_rect, Color.White);
                spriteBatch.Draw(lower, l_rect, Color.White);
            }
            else
            {
                spriteBatch.Draw(upper, u_rect, Color.White);
                spriteBatch.Draw(middle, m_rect, middle_color * alpha);
                spriteBatch.Draw(lower, l_rect, Color.White);
            }
        }

        public bool InTransition
        {
            get { return inTransition; }
        }
    }
}
